package atom.core0.util;

/**
 * <p>Title: </p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2011.12</p>
 * @author 赵明才
 * @version 1.0
 */
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;

public class StreamUtil
{
	/**
	 * @方法功能 byte 转为 InputStream
	 * @param 字节数组
	 * @return InputStream
	 * @throws Exception
	 */
	public static InputStream byte2InputStream(byte[] b) throws Exception
	{
		InputStream is = new ByteArrayInputStream(b);
		return is;
	}

	/**
	 * @方法功能 InputStream 转为 byte
	 * @param InputStream
	 * @return 字节数组
	 * @throws Exception
	 */
	public static byte[] inputStream2Byte(InputStream is) throws Exception
	{
		if (is == null)
			return null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int len = 0;
		byte[] b = new byte[1024];
		while ((len = is.read(b, 0, b.length)) != -1)
		{
			baos.write(b, 0, len);
		}
		byte[] buffer = baos.toByteArray();
		baos.close();

		return buffer;
		/*
		int count = 0;
		while (count == 0)
		{
			count = is.available();
		}
		byte[] b = new byte[count];
		inStream.read(b);
		return b;
		*/
	}

	//会改变原始数据 reader.readLine的返回值并不带换行 \r \n都变成了\n
	public static String getStringFromInputStreamByLine(InputStream is,String charset) throws IOException
	{
		if (is == null)
			return null;
		BufferedReader reader = new BufferedReader(new InputStreamReader(is,charset));
		String result = "";
		String line;
		while ((line = reader.readLine()) != null)
		{
			result += line+"\n";
		}
		
		return result;
	}
	//原始的读 不改变任何
	public static String getStringFromInputStream(InputStream is,String charset) throws IOException
	{
		if (is == null)
			return null;
		BufferedReader reader = new BufferedReader(new InputStreamReader(is,charset));
		int nread;
		StringBuffer sb = new StringBuffer();
		char cbuf[] =  new char[1024];
		while ((nread = reader.read(cbuf)) != -1)
		{
			sb.append(cbuf, 0, nread);
		}
		return sb.toString();
	}

	public static byte[] getByteArrayFromInputStream(InputStream is) throws IOException
	{
		if (is == null)
			return null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int len = 0;
		byte[] b = new byte[1024];
		while ((len = is.read(b, 0, b.length)) != -1)
		{
			baos.write(b, 0, len);
		}
		byte[] buffer = baos.toByteArray();
		baos.close();

		return buffer;
	}


	public static Serializable getJavaObjectFromInputStream(InputStream is) throws Exception
	{
		if (is == null)
			return null;
		java.io.ObjectInputStream oInput = null;
		Serializable returnObj = null;
		oInput = new java.io.ObjectInputStream(is);
		returnObj = (Serializable) oInput.readObject();

		return returnObj;

	}

	public static void printInputStream(InputStream is)
	{
		BufferedInputStream in = new BufferedInputStream(is);
		Reader r = new InputStreamReader(in);
		int c;
		System.out.println("==================Beging====================");
		try
		{
			while ((c = r.read()) != -1)
				System.out.print((char) c);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		System.out.println("===================End======================");

	}
}
